package us.kg.kotoriforest.schedule.dao.impl;

import us.kg.kotoriforest.schedule.dao.BaseDAO;
import us.kg.kotoriforest.schedule.dao.SysScheduleDAO;
import us.kg.kotoriforest.schedule.pojo.SysSchedule;

import java.sql.SQLException;
import java.util.List;

/**
 * ClassName: us.kg.kotoriforest.schedule.dao.impl.SysScheduleDAOImpl </br>
 * Description: sys_schedule 表的 DAO 实现类
 *
 * @author Kotori rotroutine@163.com
 * @since 2024/12/1
 */
public class SysScheduleDAOImpl extends BaseDAO implements SysScheduleDAO {
    @Override
    public SysSchedule selectBySid(int sid) {
        String sql = "SELECT sid, uid, title, completed FROM sys_schedule WHERE sid = ?";
        try {
            return executeQueryGetFirstElement(SysSchedule.class, sql, sid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<SysSchedule> selectAll() {
        String sql = "SELECT sid, uid, title, completed FROM sys_schedule";
        try {
            return executeQuery(SysSchedule.class, sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int insert(SysSchedule sysSchedule) {
        String sql = "INSERT INTO sys_schedule(uid, title, completed) VALUES(?, ?, ?)";
        try {
            return executeUpdate(sql, sysSchedule.getUid(), sysSchedule.getTitle(), sysSchedule.getCompleted());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int update(SysSchedule sysSchedule) {
        String sql = "UPDATE sys_schedule SET uid = ?, title = ?, completed = ? WHERE sid = ?";
        try {
            return executeUpdate(sql, sysSchedule.getUid(), sysSchedule.getTitle(), sysSchedule.getCompleted(), sysSchedule.getSid());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deleteBySid(int sid) {
        String sql = "DELETE FROM sys_schedule WHERE sid = ?";
        try {
            return executeUpdate(sql, sid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int countAll() {
        String sql = "SELECT COUNT(sid) FROM sys_schedule";
        try {
            return executeQueryGetBasicElement(Integer.class, sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
